// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database();
const jiyou = db.collection('jiyou')
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
   const {index1,index2} = event;
   let jiang;
   let rand = Math.random();
   switch(true){
     case(rand<0.1):
     jiang=1;
     break;
     case(rand>=0.1&&rand<0.25):
     jiang=2;
     break;
     case(rand>=0.25&&rand<0.55):
     jiang=3;
     break;
     case (rand >=0.55&&rand<1):
     jiang=4;
     break;
   }
   var user = await jiyou.where({
     '_openid':wxContext.OPENID
   }).get();
  var word = user.data[0].words
  word[index1].number -= 1;
  word[index2].number -= 1;
  
    let result = await jiyou.where({
      '_openid': wxContext.OPENID
    }).update({
      data:{
       words:word,
       'prize':1
      }
    })
  return {
    event,
    // index1:index1
    word,
    index1,
    index2,
    jiang
  }
}